package com.lanyan.system.controller.api;

import com.lanyan.common.annotation.RepeatSubmit;

import java.math.BigDecimal;

import com.lanyan.common.utils.SecurityUtils;
import com.lanyan.system.domain.Sku;
import com.lanyan.system.service.IProductService;
import com.lanyan.system.service.ISkuService;
import io.swagger.annotations.Api;

import java.util.List;
import java.util.Date;

import com.lanyan.common.core.domain.R;
import com.lanyan.common.core.domain.PageR;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import io.swagger.annotations.ApiOperation;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.core.domain.R;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import springfox.documentation.annotations.ApiIgnore;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.domain.Shopping;
import com.lanyan.system.domain.vo.ShoppingVo;
import com.lanyan.system.service.IShoppingService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 购物车Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@Api(tags = {"购物车"})
@RequestMapping("/api/shopping")
public class ApiShoppingController extends BaseController {
    @Autowired
    private IShoppingService shoppingService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ISkuService skuService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车列表")
    public R<List<Shopping>> list(@ApiIgnore Shopping shopping) {
        shopping.setAccountId(SecurityUtils.getUserId());
        List<Shopping> list = shoppingService.selectShoppingList(shopping);
        for (Shopping item : list) {
            item.setProduct(productService.getById(item.getProductId()));
            item.setSku(skuService.getById(item.getSkuId()));
        }
        return R.ok(list);
    }

    /**
     * 新增购物车
     */
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping("saveOrUpdate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "skuId", value = "所属规格", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "productId", value = "所属商品", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "num", value = "数量", dataType = "Long", dataTypeClass = Long.class),
    })
    @ApiOperation(value = "新增购物车", notes = "传id走直接修改，不传id会自适应追加")
    public R<Shopping> saveOrUpdate(@ApiIgnore @RequestBody Shopping shopping) {
        if (shopping.getShoppingId() != null) {
            shoppingService.updateById(shopping);
            return R.ok(shopping);
        }
        Shopping old = shoppingService.lambdaQuery()
                .eq(ShoppingVo::getAccountId, SecurityUtils.getUserId())
                .eq(ShoppingVo::getProductId, shopping.getProductId())
                .eq(ShoppingVo::getSkuId, shopping.getSkuId())
                .one();
        if (old != null) {
            old.setNum(old.getNum() + shopping.getNum());
            shoppingService.updateById(old);
            return R.ok(old);
        }
        StringUtils.insertRequired(shopping);
        shopping.setAccountId(SecurityUtils.getUserId());
        shoppingService.insertShopping(shopping);
        return R.ok(shopping);
    }

    /**
     * 删除购物车
     */
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @GetMapping("/del")
    @ApiOperation(value = "删除购物车", notes = "id  多个用,拼接")
    public AjaxResult remove(String shoppingIds) {
        String[] split = shoppingIds.split(",");
        for (String id : split) {
            shoppingService.removeById(id);
        }
        return success("删除成功");
    }

    /**
     * 获取合计
     */
    @GetMapping("/getSum")
    @ApiOperation(value = "获取合计价格", notes = "id  多个用,拼接")
    public AjaxResult getSum(String shoppingIds) {
        String[] split = shoppingIds.split(",");
        BigDecimal sum = BigDecimal.ZERO;
        if (StringUtils.isNotEmpty(shoppingIds) && split.length != 0 ) {
            for (String id : split) {
                Shopping shopping = shoppingService.getById(id);
                Sku sku = skuService.getById(shopping.getSkuId());
                BigDecimal decimal = sku.getPrice().multiply(new BigDecimal(shopping.getNum()));
                sum = sum.add(decimal);
            }
        }
        return success(sum);
    }

    /**
     * 获取购物车数量
     */
    @GetMapping("/getCount")
    @ApiOperation(value = "获取购物车数量")
    public AjaxResult getCount() {
        Integer count = shoppingService.lambdaQuery()
                .eq(ShoppingVo::getAccountId, SecurityUtils.getUserId())
                .eq(ShoppingVo::getDelFlag, "0")
                .count();
        return success(count);
    }


}
